package com.huanke.iot.api.controller.newapp;


import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.huanke.iot.api.controller.h5.response.LocationVo;
import com.huanke.iot.api.controller.h5.response.WeatherVo;
import com.huanke.iot.api.controller.newapp.model.*;
import com.huanke.iot.api.controller.newapp.resp.NewAppChildDeviceListResp;
import com.huanke.iot.api.service.device.basic.DeviceService;
import com.huanke.iot.api.service.newapp.NewAppDeviceService;
import com.huanke.iot.api.util.IpUtils;
import com.huanke.iot.base.api.ApiResponse;
import com.huanke.iot.base.util.LocationUtils;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/newApp")
public class NewAppDeviceController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    public NewAppDeviceService newAppDeviceService;

    @Autowired
    private LocationUtils locationUtils;


    @GetMapping("/iot/high/childDeviceList/{deviceId}")
    public ApiResponse<NewAppChildDeviceListResp> queryChildByDeviceId(@PathVariable String deviceId) {

        if (StringUtils.isEmpty(deviceId)) {
            return ApiResponse.PARAM_ERROR;
        }
        NewAppChildDeviceListResp newAppChildDeviceListResp = new NewAppChildDeviceListResp();

        List<NewAppChildDeviceDTO> newAppChildDeviceDTOS = newAppDeviceService.queryChildDeviceList(deviceId);

        newAppChildDeviceListResp.setChildDeviceDTOList(newAppChildDeviceDTOS);

        return new ApiResponse<>(newAppChildDeviceListResp);
    }

    @PostMapping("/iot/high/deleteFile")
    public ApiResponse<Boolean> deleteModelConfig(@RequestBody List<Integer> configIds) {
        if (CollectionUtils.isEmpty(configIds)) {
            return ApiResponse.PARAM_ERROR;
        }
        Boolean deleteRet = newAppDeviceService.deleteFile(configIds);
        return new ApiResponse<>(deleteRet);
    }

    @GetMapping("/iot/high/queryModelList")
    public ApiResponse<List<DeviceModelDTO>> getAllModels(String modelId) {
        List<DeviceModelDTO> deviceModelDTOS = newAppDeviceService.queryAllModels(modelId)
                .stream().map(deviceModelPo -> {
                    DeviceModelDTO deviceModelDTO = new DeviceModelDTO();
                    deviceModelDTO.setModelId(deviceModelPo.getId());
                    deviceModelDTO.setModelName(deviceModelPo.getName());
                    deviceModelDTO.setNo(deviceModelPo.getNo());
                    return deviceModelDTO;
                }).collect(Collectors.toList());
        newAppDeviceService.fillModelFile(deviceModelDTOS);
        return new ApiResponse<>(deviceModelDTOS);
    }

    @PostMapping("/iot/high/updateDeviceInfo")
    public ApiResponse<Boolean> updateDeviceInfo(@RequestBody DeviceUpdateInfoDTO deviceUpdateInfoDTO) {
        if (CollectionUtils.isEmpty(deviceUpdateInfoDTO.getChildDataList())) {
            return ApiResponse.PARAM_ERROR;
        }

        Boolean updateRet = newAppDeviceService.updateDeviceInfo(deviceUpdateInfoDTO);

        return new ApiResponse<>(updateRet);
    }

    @PostMapping("/iot/high/deleteChild")
    public ApiResponse<Boolean> deleteChild(@RequestBody DeleteChildDTO deleteChildDTO) {

        if (deleteChildDTO.getHostDeviceId() == null || CollectionUtils.isEmpty(deleteChildDTO.getChildIdList())) {
            return ApiResponse.PARAM_ERROR;
        }
        Boolean updateRet = newAppDeviceService.deleteChild(deleteChildDTO);

        return new ApiResponse<>(updateRet);
    }


    @PostMapping("/iot/high/uploadModelConfig")
    public ApiResponse<Boolean> uploadModelConfig(@RequestParam("file") MultipartFile file, @RequestParam("modelId") Integer modelId) {

        Boolean updateRet = newAppDeviceService.uploadModelConfig(modelId, file);

        return new ApiResponse<>(updateRet);
    }


    @PostMapping("/iot/high/addModifyScene")
    public ApiResponse<Boolean> addModifyScene(@RequestBody DeviceSceneDTO deviceSceneDTO) {

        if (deviceSceneDTO == null || deviceSceneDTO.getHostDeviceId() == null
                || CollectionUtils.isEmpty(deviceSceneDTO.getChildSceneList())
                ) {
            return ApiResponse.PARAM_ERROR;
        }
        Boolean updateRet = newAppDeviceService.addModifyScene(deviceSceneDTO);

        return new ApiResponse<>(updateRet);
    }

    @GetMapping("/iot/high/getSceneById/{deviceId}")
    public ApiResponse<DeviceSceneListDTO> getSceneById(@PathVariable Integer deviceId) {
        if (deviceId == null) {
            return ApiResponse.PARAM_ERROR;
        }
        DeviceSceneListDTO deviceSceneDTO = newAppDeviceService.getSceneById(deviceId);

        return new ApiResponse(deviceSceneDTO);

    }

    @PostMapping("/iot/high/deletescene")
    public ApiResponse<Boolean> deleteScene(@RequestBody DeleteSceneDTO request) {
        if (request.getHostDeviceId()==null || request.getSceneNameList().size() == 0) {
            return ApiResponse.PARAM_ERROR;
        }
        Boolean deleteRet = newAppDeviceService.deleteScene(request);
        return new ApiResponse(deleteRet);

    }

//    @GetMapping("/iot/high/getWeather")
//    public ApiResponse<JSONObject> getWeather(HttpServletRequest request){
//        String ip = IpUtils.getIpAddr(request);
//
//        JSONObject jsonObject =  locationUtils.getWeather(ip, true);
//
//        String nowTime = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
//
//        jsonObject.put("nowTime" ,nowTime);
//        return new ApiResponse<>(jsonObject);
//    }

    @RequestMapping("/iot/high/getWeatherAndLocation/{deviceId}")
    public ApiResponse<JSONObject> queryDeviceLocation(@PathVariable("deviceId") Integer deviceId) {
        LocationVo locationVo = deviceService.queryDeviceLocation(deviceId);
        //对地区单独处理
        if (locationVo.getLocation()!=null)
        {
            String[] locationArray = locationVo.getLocation().split(",");
            //locationVo.setArea(locationArray[0]+" "+locationArray[1]+" "+locationArray[2]);
            locationVo.setProvince(locationArray[0]);
            locationVo.setCity(locationArray[1]);
            locationVo.setArea(locationArray[2]);
        }
        WeatherVo weatherVo = deviceService.queryDeviceWeather(deviceId);
        JSONObject jsonObject=new JSONObject();
        String nowTime = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        jsonObject.put("nowTime" ,nowTime);
        jsonObject.put("Location",locationVo);
        jsonObject.put("Weather",weatherVo);
        return new ApiResponse<>(jsonObject);
    }



}
